﻿using Dapper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace NewBee.Core.Data.SQLBuilder
{
    public class Update
    {
        private static string UPDATE_SQL = @"UPDATE {0} SET {1} WHERE {2}";
        private DbHelper _DbHelper = null;

        private string _TableName = string.Empty;
        private string _Where = string.Empty;
        private Hashtable _Columns = null;
        private DynamicParameters _Parameters;

        public Update(DbHelper dbHelper, string tableName)
        {
            _DbHelper = dbHelper;
            _TableName = tableName;
        }

        public Update Where(string where)
        {
            _Where = where;
            return this;
        }

        public Update Column(Hashtable columns)
        {
            this._Columns = columns;
            return this;
        }
        public int Execute()
        {
            if (_Columns == null)
                return -1;

            if (string.IsNullOrEmpty(_Where))
                _Where = " 1=1 ";
            string columnNames = "";
            _Parameters = new DynamicParameters();
            foreach (string key in _Columns.Keys)
            {
                columnNames += key + "=@" + key + ",";
                _Parameters.Add("@" + key, _Columns[key]);
            }
            string sql = string.Format(UPDATE_SQL, _TableName, columnNames.TrimEnd(','), _Where);
            return _DbHelper.Execute(sql, _Parameters);
        }
    }

    public class Update_T<T>
    {
        /// <summary>
        /// 帮助类
        /// </summary>
        private DbHelper _DbHelper = null;

        /// <summary>
        /// 条件
        /// </summary>
        private string _Where = string.Empty;

        private T _Model;

        /// <summary>
        /// 参数
        /// </summary>
        private DynamicParameters _Parameters;

        public Update_T(DbHelper dbHelper, T model)
        {
            _DbHelper = dbHelper;
            _Model = model;
        }

        public Update_T<T> Where(string where)
        {
            _Where = where;
            return this;
        }

        /// <summary> 
        /// 修改 
        /// </summary> 
        /// <typeparam name="T"></typeparam> 
        /// <param name="model"></param> 
        /// <returns></returns> 
        public string UpdateSQL(T model)
        {
            StringBuilder sbColumns = new StringBuilder();

            PropertyInfo[] _PropertyInfos = model.GetType().GetProperties();
            _Parameters = new DynamicParameters();
            foreach (PropertyInfo _PropertyInfo in _PropertyInfos)
            {
                if (null != _PropertyInfo.GetValue(model, null))
                {
                    sbColumns.Append(_PropertyInfo.Name + "=@" + _PropertyInfo.Name + ",");
                    _Parameters.Add("@" + _PropertyInfo.Name, _PropertyInfo.GetValue(model));
                }
            }

            if (sbColumns.Length > 0)
            {
                sbColumns.Remove(sbColumns.ToString().Length - 1, 1);
            }
            string strSql = "UPDATE {0} SET {1} WHERE {2}";

            strSql = string.Format(strSql, model.GetType().Name, sbColumns.ToString(), _Where);

            return strSql;
        }

        /// <summary>
        /// 执行修改操作
        /// </summary>
        /// <returns></returns>
        public int Execute()
        {
            string sql = UpdateSQL(_Model);

            return _DbHelper.Execute(sql, _Parameters);
        }
    }
}
